package com.hnhcyy.goods.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.hnhcyy.b2b.common.util.MyBeanUtils;
import com.hnhcyy.goods.pojo.*;
import com.hnhcyy.goods.service.*;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 为后台提供服务
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/goodsAdmin")
public class GoodsAdminApi {
	
	@Autowired
	GoodsAdminService goodsAdminService;
	@Autowired
	SpkfkDescAdminService spkfkDescService;
	@Autowired
	GoodsPreAdminService goodsPreService;
	@Autowired
	RiseInPriceAdminService riseInPriceService;
	@Autowired
	SpkfklackAdminService spkfklackAdminService;
	@Autowired
	SpTestReportAdminService spTestReportAdminService;
	
	/**
	 * 获取所有商品列表并分页
	 * @param queryParam
	 * @return
	 */
	@ApiOperation(value = "获取所有商品列表", notes = "根据条件查询并分页，用于admin后台")
	@PostMapping(value = "/goods/listAllGoods")
	public String listAllGoods(@RequestBody GoodsAdminQueryParam queryParam){
		
		return goodsAdminService.listAllGoods(queryParam);
	}
	
	/**
	 * 更新商品信息
	 * @param goods
	 * @return
	 */
	@ApiOperation(value = "更新单个商品信息", notes = "用于admin后台调用,更新商品信息")
	@PostMapping(value = "/goods/updateGoods")
	public String updateGoods(@RequestBody Goods goods){
		
		return goodsAdminService.updateGoods(goods);
	}
	
	@ApiOperation(value = "更新单个商品价格", notes = "用于admin后台调用,更新商品价格信息")
	@PostMapping(value = "/goods/updateGoodsFPrice")
	public String updateGoodsFprice(@RequestBody Goods goods){
		
		return goodsAdminService.updateGoodsFprice(goods);
	}
	
	/**
	 * 更新商品图片信息
	 * @param goods
	 * @return
	 */
	@ApiOperation(value = "更新单个商品信息", notes = "用于admin后台调用,更新商品信息")
	@PostMapping(value = "/goods/updateGoodsPicture")
	public String updateGoodsPicture(@RequestBody Goods goods){
		
		return goodsAdminService.updateGoodsPicture(goods);
	}
	
	/**
	 * 更新扩展表信息
	 * @param goodsExtend
	 * @return
	 */
	@ApiOperation(value = "更新扩展表信息", notes = "更新扩展表信息")
	@PostMapping(value = "/goods/updateGoodsExtend")
	public String updateXsqy(@RequestBody GoodsExtend goodsExtend){
		return goodsAdminService.updateGoodsExtend(goodsExtend);
	}
	
	/**
	 * 插入或更新goods表的一条记录
	 * @author sheyongjun
	 * @date 2019年2月19日
	 */
	@ApiOperation(value = "同步goods表的数据", notes = "同步goods表的数据")
	@PostMapping("/goods/update_save")
	public boolean updateOrSave(String text) {
		System.out.println(text);
		Goods goods = MyBeanUtils.stringConverToBean(text, Goods.class);
		return goodsAdminService.updateOrSaveGoods(goods);
	}
	
	/**
	 * 更新商品价格
	 * @author sheyongjun
	 * @date 2019年2月19日
	 */
	@ApiOperation(value = "更新商品价格", notes = "更新商品价格")
	@PostMapping("/goods/updatePrice")
	public boolean updatePrice(String text) {
		if(StringUtils.isBlank(text)) {
			return true;
		}
		long starttime = System.currentTimeMillis();
		// 解析json
		JSONObject jsonObject = JSONObject.parseObject(text);
		JSONArray array = jsonObject.getJSONArray("FEntry");
		List<Goods> goodsList = new ArrayList<>();
		/*array.parallelStream().forEach(e -> {
			
		});*/
		for(int i = 0, size = array.size(); i < size; i++) {
			JSONObject jsonObj = array.getJSONObject(i);
			String spid = jsonObj.getString("FMatNumber");
			if(StringUtils.isBlank(spid)) {
				// spid为必须字段
				return false;
			}
			// 抱团价
			Double price = jsonObj.getDouble("FPrice");
			// 零售价
			Double lingshoujia = jsonObj.getDouble("FLSPrice");
			// 拆零价
			Double priceChl = jsonObj.getDouble("FCLPrice");
			Goods goods = new Goods();
			goods.setSpid(spid);
			goods.setPrice(price);
			goods.setLingshoujia(lingshoujia);
			goods.setPriceChl(priceChl);
			goodsList.add(goods);
			// 更新单个商品
			/*boolean isSuccess = goodsAdminService.updateOrSaveGoods(goods);
			if(!isSuccess) {
				return false;
			}*/
		}
		// 批量更新
		int affectRows = goodsAdminService.updateGoodsList(goodsList);
		long endtime = System.currentTimeMillis();
		System.out.println("=======更新商品价格耗时：" + (endtime - starttime) / 1000.0 + "s；影响行数：" + affectRows);
		return true;
	}
	
	/**
	 * 获取商品详情
	 * @param spid
	 * @return
	 * @throws JsonProcessingException
	 */
	@ApiOperation(value = "获取商品详情", notes = "获取商品详情，用于admin后台")
	@GetMapping(value = "/goodsDetail/selectGoodsDetail")
	public String selectGoodsDetail(@RequestParam("spid") String spid){
		
		return spkfkDescService.selectGoodsDetail(spid);
	}
	
	/**
	 * 更新商品详情
	 * @param goodsDetail
	 * @return
	 */
	@ApiOperation(value = "更新商品详情", notes = "更新商品详情，用于admin后台")
	@PostMapping(value = "/goodsDetail/updateGoodsDetail")
	public String updateGoodsDetail(@RequestBody SpkfkDesc goodsDetail){
		return spkfkDescService.updateGoodsDetail(goodsDetail);
	}
	
	/**
	 * 获取即将到货商品列表并分页
	 * @param queryParam
	 * @return
	 * @throws JsonProcessingException 
	 */
	@ApiOperation(value = "获取即将到货商品列表", notes = "根据条件查询并分页，用于admin后台")
	@PostMapping(value = "/goodsPre/listGoodsPre")
	public String listGoodsPre(@RequestBody GoodsPre queryParam){
		
		return goodsPreService.listGoodsPre(queryParam);
	}

	/**
	 * 新增即将到货
	 * @param goodsPre
	 * @return
	 */
	@ApiOperation(value = "新增即将到货", notes = "新增即将到货")
	@PostMapping(value = "/goodsPre/insertGoodsPre")
	public String insertGoodsPre(@RequestBody GoodsPre goodsPre){
		
		return goodsPreService.insertGoodsPre(goodsPre);
	}
	
	/**
	 * 更新即将到货
	 * @param goodsPre
	 * @return
	 */
	@ApiOperation(value = "更新即将到货", notes = "更新即将到货")
	@PostMapping(value = "/goodsPre/updateGoodsPre")
	public String updateGoodsPre(@RequestBody GoodsPre goodsPre){
		
		return goodsPreService.updateGoodsPre(goodsPre);
	}
	
	/**
	 * 删除即将到货
	 * @param spid
	 * @return
	 */
	@ApiOperation(value = "删除即将到货", notes = "删除即将到货")
	@GetMapping(value = "/goodsPre/deleteGoodsPre")
	public String deleteGoodsPre(@RequestParam String spid){
		
		return goodsPreService.deleteGoodsPre(spid);
	}
	
	/**
	 * 查询即将涨价商品列表
	 * @param queryParam
	 * @return
	 */
	@ApiOperation(value = "查询即将涨价商品列表", notes = "查询即将涨价商品列表")
	@PostMapping(value = "/goodsRisePrice/listGoodsRisePrice")
	public String listGoodsRisePrice(@RequestBody GoodsAdminQueryParam queryParam){

		return riseInPriceService.listRiseInPrice(queryParam);
	}
	
	/**
	 * 更新即将涨价记录
	 * @param param
	 * @return
	 */
	@ApiOperation(value = "更新即将涨价商品列表", notes = "更新即将涨价商品列表")
	@PostMapping(value = "/goodsRisePrice/updateGoodsRisePrice")
	public String deleteGoodsRisePrice(@RequestBody GoodsExtend param){

		return riseInPriceService.updateRiseInPrice(param);
	}

	
	/**
	 * 查询缺货记录列表
	 * @param queryParam
	 * @return
	 */
	@ApiOperation(value = "查询缺货记录列表", notes = "查询缺货记录列表")
	@PostMapping(value = "/goodsLack/listGoodsLack")
	public String listGoodsLack(@RequestBody Spkfklack queryParam){
		
		return spkfklackAdminService.listSpkfklack(queryParam);
	}
	
	/**
	 * 新增缺货记录
	 * @param queryParam
	 * @return
	 */
	@ApiOperation(value = "新增缺货记录", notes = "新增缺货记录")
	@PostMapping(value = "/goodsLack/insertGoodsLack")
	public String insertGoodsLack(@RequestBody Spkfklack spkfklack){
		
		return spkfklackAdminService.insertSpkfklack(spkfklack);
	}
	
	/**
	 * 更新缺货记录
	 * @param queryParam
	 * @return
	 */
	@ApiOperation(value = "更新缺货记录", notes = "更新缺货记录")
	@PostMapping(value = "/goodsLack/updateGoodsLack")
	public String updateGoodsLack(@RequestBody Spkfklack spkfklack){
		
		return spkfklackAdminService.updateSpkfklack(spkfklack);
	}
	
	/**
	 * 查询质检记录列表
	 * @param queryParam
	 * @return
	 */
	@ApiOperation(value = "查询质检记录", notes = "查询质检记录列表")
	@PostMapping(value = "/goodsTestReport/listGoodsTestReport")
	public String listGoodsTestReport(@RequestBody SpTestReport queryParam){
		
		return spTestReportAdminService.listSpTestReport(queryParam);
	}
	
	/**
	 * 新增质检记录
	 * @param param
	 * @return
	 */
	@ApiOperation(value = "新增质检记录", notes = "新增质检记录")
	@PostMapping(value = "/goodsTestReport/insertGoodsTestReport")
	public String insertGoodsTestReport(@RequestBody SpTestReport spTestReport){

		return spTestReportAdminService.insertSpTestReport(spTestReport);
	}
	
	/**
	 * 更新质检记录
	 * @param spTestReport
	 * @return
	 */
	@ApiOperation(value = "更新质检记录", notes = "更新质检记录")
	@PostMapping(value = "/goodsTestReport/updateGoodsTestReport")
	public String updateGoodsTestReport(@RequestBody SpTestReport spTestReport){
		return spTestReportAdminService.updateSpTestReport(spTestReport);
	}
	
	/**
	 * 删除质检记录
	 * @param fid
	 * @return
	 */
	@ApiOperation(value = "删除质检记录", notes = "删除质检记录")
	@GetMapping(value = "/goodsTestReport/deleteGoodsTestReport")
	public String deleteGoodsTestReport(@RequestParam Integer fid){
		return spTestReportAdminService.deleteSpTestReport(fid);
	}
	
	@GetMapping(value =  "/spid_list_on_sell")
	public List<String> spidListOnSell(){
		return goodsAdminService.getSpidListOnSell();
	}
	
	
}
